#!/usr/bin/ruby
#
# VID Very Important Dates
#
# This script generates a countdown webpage using the data provided
# In dates.txt
# To find out all this work together read the README
require 'date.rb'
class Event
	def initialize(desc, hapenin, blurb)
	    @desc = desc
	    #puts "spliting #{hapenin}"
	    $dtok = hapenin.chomp.split(' ')
	    @date = Date.new($dtok[2].to_i ,$dtok[0].to_i,$dtok[1].to_i)
	    #we keep a second date to sort by month and date only
	    #if the event has passed switch to next year, else stay in the same
	    #year
	    fakeDate = Date.new(Date.today.year,@date.mon, @date.day)
	    if (@date.day == Date.today.day) && (@date.mon == Date.today.mon)
		@esdia = true
	    else
		@esdia = false
	    end
	    if fakeDate < Date.today
		@date2 = Date.new(Date.today.year + 1 ,$dtok[0].to_i,$dtok[1].to_i)
	    else
		@date2 = Date.new(Date.today.year,$dtok[0].to_i,$dtok[1].to_i)
	    end
	    @blurb = blurb
	end
	def getDesc
		@desc
	end
	def getDate
		@date
	end
	def getDate2
		@date2
	end
	def getBlurb
		@blurb
	end
	def getEsdia
		@esdia
	end
end

def configure( events )
    @conf = IO.readlines("/tmp/dates.txt")
    @conf.each do |line|
        #collect the value pairs ignore empty lines or lines that start with a hash
         if line[0] == 35  || line[0] == 10 #this is not very readable but 35 is the ascii for "#"
            next
         else
	# we use comma separated values
            tokens = line.chomp.split(',')
            events.push(Event.new(tokens[0], tokens[1], tokens[2]))
         end 
    end  
end

#start building webpage
daEvents = []
configure(daEvents)
#behold! I'm sorting the dates by the magic of date.rb library!
daEvents.sort! {|a,b| a.getDate2 <=> b.getDate2 }


#now print them already sorted
#daEvents.each do |e|
#    puts e.getDesc 
#end
#start building webpage
#read the template
     shadegrey = ["#F7F7F7", "#F0F0F0", "#E8E8E8","#E0E0E0", "#DCDCDC","#D3D3D3","#D1D1D1","#CDCDCD","#C4C4C4","#BEBEBE","#BABABA","#B3B3B3","#AAAAAA","#A8A8A8","#A3A3A3","#9C9C9C","#949494","#8C8C8C"]
    @templ = IO.readlines("/tmp/vid.template.html")
    @templ.each do |l|
	if  l =~ /put date here/ 
		puts Date.today.strftime("%A %B %d %Y")
	end
	if  l =~ /start script here/ 
	    daEvents.each do |e|
		#puts "<tr  bgcolor=\"#DCDCDD\">\n"
		if shadegrey.length == 0
		    shadegrey.push("#8C8C8C")
		end
		puts "<tr  bgcolor=\"#{shadegrey.shift}\">\n"
		puts "<td>\n"
	        puts e.getDesc 
		puts "</td>\n"
		puts "<td>\n"
	        puts "<a>#{e.getDate.strftime("%B")} #{e.getDate.day}<span>#{e.getBlurb}</span></a>"
		puts "</td>\n"
		if e.getEsdia
		    puts "<td bgcolor=\"#EEE8AA\">\n"
		    puts "Today"
		else
		    puts "<td>\n"
		   puts "<a><? countdown(2,#{e.getDate2.year}, #{e.getDate2.month}, #{e.getDate2.day}); ?><span>Event started: <? countdown(2,#{e.getDate.year}, #{e.getDate.month}, #{e.getDate.day}); ?></span></a>"
		end
		puts "</td>\n"
		puts "</tr>\n"
	    end
		    else
	    puts l
	end
    end
    
#finish building webpage
